# 初始化flask应用对象
from flask import Flask
from config import config_map
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def create_app(config_name):
    app = Flask(__name__)
    obj = config_map.get(config_name)
    app.config.from_object(obj)  # 数据库链接地址
    db.init_app(app)  # 创建完app对象顺带把数据库创建好

    from flask_shop.user import user
    app.register_blueprint(user)  # 注册蓝图

    from flask_shop.menu import menu
    app.register_blueprint(menu)

    from flask_shop.role import role
    app.register_blueprint(role)

    from flask_shop.category import category
    app.register_blueprint(category)

    from flask_shop.category import attribute
    app.register_blueprint(attribute)

    from flask_shop.goods import goods
    app.register_blueprint(goods)

    from flask_shop.order import order
    app.register_blueprint(order)

    return app
# db = SQLAlchemy(app)  # 如果不把app传进去，他不知道链接哪个数据库
# 加载了配置文件后再去初始化对象
# db.init_app(app)  另一种写法效果同上


#
# if __name__ == '__main__':
#     app.run()
